<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;


use think\Log;


//APS
class Wuaps extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();

    }

    //http://wu.test/api/wuaps/znpc
    public function znpc()
    {
        set_time_limit(0); //执行时间无限
        ini_set('memory_limit', '-1'); //内存无限

        $list = \app\admin\model\wu\Planlog::where("switch", 1)->where("state", "未完成")->where("finishnum < maxnum")->select();
        $list = collection($list)->toArray();
        foreach ($list as &$row) {
            $row = $this->addArrayinfo($row);
            //未完成的工序
            for ($i = $row['finishnum']; $i < 12; $i++) {
                $row['start_times'][$i] = 0;
                $row['end_times'][$i] = 0;
                if ($i == $row['finishnum']) {
                    //工序是否已开始做
                    $pstate = \app\admin\model\wu\Procedurestate::where("planlog_id", $row['id'])->where("procedure_id", $row['procedures'][$i]['id'])->where("state", "1")->find();
                    if($pstate) {
                        $row['start_times'][$i] = strtotime($pstate['startdate']);
                    }
                }
            }
        }

        $this->success("ok");
    }

    function addArrayinfo($row)
    {
        $start_time = [];
        $end_time = [];
        $start_time[] = $row['procedure1_startdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure1_startdate']);
        $end_time[] = $row['procedure1_finishdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure1_finishdate']);
        $start_time[] = $row['procedure2_startdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure2_startdate']);
        $end_time[] = $row['procedure2_finishdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure2_finishdate']);
        $start_time[] = $row['procedure3_startdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure3_startdate']);
        $end_time[] = $row['procedure3_finishdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure3_finishdate']);
        $start_time[] = $row['procedure4_startdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure4_startdate']);
        $end_time[] = $row['procedure4_finishdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure4_finishdate']);
        $start_time[] = $row['procedure5_startdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure5_startdate']);
        $end_time[] = $row['procedure5_finishdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure5_finishdate']);
        $start_time[] = $row['procedure6_startdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure6_startdate']);
        $end_time[] = $row['procedure6_finishdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure6_finishdate']);
        $start_time[] = $row['procedure7_startdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure7_startdate']);
        $end_time[] = $row['procedure7_finishdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure7_finishdate']);
        $start_time[] = $row['procedure8_startdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure8_startdate']);
        $end_time[] = $row['procedure8_finishdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure8_finishdate']);
        $start_time[] = $row['procedure9_startdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure9_startdate']);
        $end_time[] = $row['procedure9_finishdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure9_finishdate']);
        $start_time[] = $row['procedure10_startdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure10_startdate']);
        $end_time[] = $row['procedure10_finishdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure10_finishdate']);
        $start_time[] = $row['procedure11_startdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure11_startdate']);
        $end_time[] = $row['procedure11_finishdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure11_finishdate']);
        $start_time[] = $row['procedure12_startdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure12_startdate']);
        $end_time[] = $row['procedure12_finishdate'] === "0000-00-00 00:00:00"? 0 : strtotime($row['procedure12_finishdate']);

        //机台id
        $procedure_manid = [];
        $procedure_manid[] = $row['procedure1_man_id'];
        $procedure_manid[] = $row['procedure2_man_id'];
        $procedure_manid[] = $row['procedure3_man_id'];
        $procedure_manid[] = $row['procedure4_man_id'];
        $procedure_manid[] = $row['procedure5_man_id'];
        $procedure_manid[] = $row['procedure6_man_id'];
        $procedure_manid[] = $row['procedure7_man_id'];
        $procedure_manid[] = $row['procedure8_man_id'];
        $procedure_manid[] = $row['procedure9_man_id'];
        $procedure_manid[] = $row['procedure10_man_id'];
        $procedure_manid[] = $row['procedure11_man_id'];
        $procedure_manid[] = $row['procedure12_man_id'];

        //manhours-- 工序工时
        //preparedate --- 原料備齊日
        $procedures = [];
        $procedures[] = array('id' => $row['procedure1_id'], 'manhours' => $row['procedure1_manhours'], 'preparedate' => $row['procedure1_preparedate']);
        $procedures[] = array('id' => $row['procedure2_id'], 'manhours' => $row['procedure2_manhours'], 'preparedate' => $row['procedure2_preparedate']);
        $procedures[] = array('id' => $row['procedure3_id'], 'manhours' => $row['procedure3_manhours'], 'preparedate' => $row['procedure3_preparedate']);
        $procedures[] = array('id' => $row['procedure4_id'], 'manhours' => $row['procedure4_manhours'], 'preparedate' => $row['procedure4_preparedate']);
        $procedures[] = array('id' => $row['procedure5_id'], 'manhours' => $row['procedure5_manhours'], 'preparedate' => $row['procedure5_preparedate']);
        $procedures[] = array('id' => $row['procedure6_id'], 'manhours' => $row['procedure6_manhours'], 'preparedate' => $row['procedure6_preparedate']);
        $procedures[] = array('id' => $row['procedure7_id'], 'manhours' => $row['procedure7_manhours'], 'preparedate' => $row['procedure7_preparedate']);
        $procedures[] = array('id' => $row['procedure8_id'], 'manhours' => $row['procedure8_manhours'], 'preparedate' => $row['procedure8_preparedate']);
        $procedures[] = array('id' => $row['procedure9_id'], 'manhours' => $row['procedure9_manhours'], 'preparedate' => $row['procedure9_preparedate']);
        $procedures[] = array('id' => $row['procedure10_id'], 'manhours' => $row['procedure10_manhours'], 'preparedate' => $row['procedure10_preparedate']);
        $procedures[] = array('id' => $row['procedure11_id'], 'manhours' => $row['procedure11_manhours'], 'preparedate' => $row['procedure11_preparedate']);
        $procedures[] = array('id' => $row['procedure12_id'], 'manhours' => $row['procedure12_manhours'], 'preparedate' => $row['procedure12_preparedate']);


        $row['procedure_manids'] = $procedure_manid;
        $row['start_times'] = $start_time;
        $row['end_times'] = $end_time;
        $row['procedures'] = $procedures;

        return $row;
    }


}





